<template>
	<view>
		<view class="log">
			<image src="../../static/logo.png" mode=""></image>
			<view class="text">
				云开发 申请授权
			</view>
			<view class="name">
				获取您的公开信息（昵称，头像等）
			</view>
		</view>
		<view class="title">
			◆ 用于更好提供以下服务 ◆
		</view>

		<!-- 服务信息 -->
		<view class="serve">
			<view class="serve_info" v-for="(item,index) in serveList" :key="index">
				<text :class="item.icon"></text>
				<text class="info_text">{{item.name}}</text>
			</view>
		</view>

		<view class="padding flex flex-direction" style="margin-top: 120rpx;">
			<button class="cu-btn bg-green margin-tb-sm lg" open-type="getUserInfo" @getuserinfo="getUserInfo">授权登录</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				serveList: [{
					'icon': 'cuIcon-brand',
					'name': '品质保证'
				}, {
					'icon': 'cuIcon-edit',
					'name': '线上订购'
				}]
			};
		},
		methods: {
			//获取用户授权
			getUserInfo(e) {
				if (e.detail.errMsg == 'getUserInfo:ok') {
					this.userInfo()
				} else {
					this.$operate.toast({
						title: '授权失败无法登录！'
					})
				}
			},
			// 获取用户信息
			userInfo() {
				let that = this
				uni.login({
					provider: 'weixin',
					success(login) {
						console.log(login);
						uni.getUserInfo({
							provider: 'weixin',
							lang: 'zh_CN',
							success(res) {
								console.log(res);

								let data = {
									code: login.code,
									signature: res.signature,
									encrypted_data: res.encryptedData,
									iv: res.iv,
									userInfo: res.userInfo
								}
								// 登录请求
								uniCloud.callFunction({
									name: 'wx_login',
									data: data
								}).then((res) => {
									console.log('微信授权成功', res);
								})
							}
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	[v-cloak] {
		display: none
	}

	page {
		background-color: #FFFFFF;
	}

	.log {
		padding-top: 80rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		image {
			height: 140rpx;
			width: 140rpx;
			border-radius: 50%;
		}

		.text {
			font-size: 35rpx;
			font-weight: 600;
			margin-top: 20rpx;
			color: #333333;
		}

		.name {
			margin-top: 20rpx;
			font-size: 30rpx;
			color: #AAAAAA;
		}
	}

	.title {
		line-height: 100rpx;
		text-align: center;
		padding-top: 30rpx;
		font-size: 35rpx;
	}

	.serve {
		margin: 20rpx 30rpx;
		background-color: #e6f9ff;
		// height: 100rpx;
		border-radius: 20rpx;
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		.serve_info {
			font-size: 35rpx;
			padding: 20rpx 20rpx;
			color: #868686;
			width: 345rpx;
			text-align: center;

			.info_text {
				margin-left: 15rpx;
			}

		}
	}
</style>
